/*eslint-disable */
import BaseController from '../../core/controllers/Base.controller';

@Inject('$log', '$scope', '$http', '$state', 'documentService', 'ngDialogService')
class DocumentItemMgtCtrl extends BaseController {
  constructor() {
    super();

    this.boxName = $state.params.name;
    this.subMenus = [];
    this.supMenu = {};
    this.documentType = $state.params.documentType;

    // 初始化页面
    this.initDocumentMgtView($state.params.boxId);
    this.init();
    $scope.$watch('documentItemMgt.selectBid', (newValue) => {
      if (newValue) {
        this.orgId = newValue.orgId;
        if (!this.subMenus[0]) {
          if (this.supMenu.stateName == 'documentItemMgt.commonDocument') {
            this.$state.go(this.supMenu.stateName, {
              type: this.supMenu.id,
              orgId: this.orgId,
              boxId: this.supMenu.id,
              name: this.supMenu.name,
            });
          } else {
            this.$state.go(this.supMenu.stateName);
          }
          return;
        }
        this.subMenus.forEach((item, index) => {
          if (item.active) {
            this.selectFolder(index);
          }
        });
      }
    });
  }

  getImgUrl() {
    return require(`../../assets/images/document/${this.boxName}.png`);
  }

  initDocumentMgtView(boxId) {
    this.documentService
      .getDetailBox(boxId)
      .then((res) => {
        if (res.success) {
          this.respData = res.rows;
          this.respData.forEach((item) => {
            if (item.remarks === '1' || item.remarks === '2' || item.remarks === '3') {
              this.SPECIAL_FOLDERS.forEach((specialFolder) => {
                if (specialFolder.name == item.name) {
                  item.stateName = specialFolder.stateName;
                }
              });
            } else {
              item.stateName = 'documentItemMgt.commonDocument';
            }

            if (item.parentId === '-1') {
              this.supMenu = item;
            } else if (item.parentId !== '-1') {
              this.subMenus.push(item);
            }

          });
        }
        // pick referred folder from main.html
        let selectedIndex = 0;
        if (this.$scope.referredBoxId !== undefined) {
          this.subMenus.forEach((item, index) => {
            if (item.id == $scope.referredBoxId) {
              selectedIndex = index;
            }
          })
        }

        this.selectFolder(selectedIndex);
      });
  }

  selectFolder(index) {
    // 没有子盒子跳转
    if (!this.subMenus[0]) {
      if (this.supMenu.stateName == 'documentItemMgt.commonDocument') {
        this.$state.go(this.supMenu.stateName, {
          type: this.supMenu.id,
          orgId: this.orgId,
          boxId: this.supMenu.id,
          name: this.supMenu.name,
        });
      } else {
        this.$state.go(this.supMenu.stateName);
      }
      return;
    }
    // 有子盒子
    this.subMenus.forEach((item) => {
      item.active = false;
    });

    const activeMenu = this.subMenus[index];
    activeMenu.active = true;

    const hrefStr = location.href;
    const str = hrefStr.indexOf('commonDocument');
    if (activeMenu.stateName === 'documentItemMgt.commonDocument') {
      if (str === -1) {
        this.$state.go(activeMenu.stateName, {
          type: activeMenu.stateName ==
            'documentItemMgt.commonDocument' ? activeMenu.id : '',
          orgId: this.orgId,
          boxId: this.supMenu.id,
          name: this.supMenu.name,
        });
      } else {
        // 发送通知
        this.$scope.$broadcast('childNotice', {
          type: activeMenu.id,
          orgId: this.orgId,
        });
      }
    } else {
      this.$state.go(activeMenu.stateName, {
        orgId: this.orgId,
      });
    }
  };

  init() {
    this.SPECIAL_FOLDERS = [
      {
        name: '花名册',
        stateName: 'documentItemMgt.personnelClassify',
      },
      {
        name: '安全会议(PAD)',
        stateName: 'documentItemMgt.meetings',
      },
      {
        name: '安全教育培训(PAD)',
        stateName: 'documentItemMgt.trainings',
      },
      {
        name: '安全技术交底(PAD)',
        stateName: 'documentItemMgt.disclosures',
      },
      {
        name: '设备信息',
        stateName: 'documentItemMgt.deviceClassify',
      },
    ]
  }

}

export default DocumentItemMgtCtrl;
